//
//  InformationCollectionViewCell.m
//  Project_A Gary
//
//  Created by leilei on 16/3/24.
//  Copyright © 2016年 www.lei.com. All rights reserved.
//

#import "InformationCollectionViewCell.h"
#import "HeadCollectionViewCell.h"
#import "MainTableViewCell.h"
#import "InformationModel.h"
#import "UIImageView+WebCache.h"
#import "ArrayMainTableViewCell.h"
#import "HeadModel.h"

#import "CircleView.h"

#import "DetailViewController.h"

@interface InformationCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

//// 轮班图
//@property (nonatomic, retain)UICollectionView *headCollectionView;

// 主要内容
@property (nonatomic, retain)UITableView *tableV;

// 轮播图
@property (nonatomic, retain)CircleView *circleV;

@end




@implementation InformationCollectionViewCell
- (void)dealloc
{
    [_tableV release];
    
    //.h建立的数组 为了传递数据(嵌套时使用)
    [_Arr release];
    [_HeadArr release];
    
    [_circleV release];
    
    [_navi release];
    [super dealloc];
    
}





// 系统初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
        
        [self createCircleView];
            }
    return self;
}




// 从VC传递数组的时候必须重写set方法
#pragma mark - 传递轮播图的数组
- (void)setHeadArr:(NSMutableArray *)HeadArr
{
    if (_HeadArr != HeadArr) {
        [_HeadArr release];
        _HeadArr = [HeadArr retain];
    }
    self.circleV.navi = self.navi;
    self.circleV.dataArr = _HeadArr;
}

- (void)setArr:(NSMutableArray *)Arr
{
    if (_Arr != Arr) {
        [_Arr release];
        _Arr = [Arr retain];
    }
    [_tableV reloadData];
}

#pragma mark - 创建轮播图
- (void)createCircleView
{
    
    self.circleV = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, 194) dataArr:_HeadArr];
    self.tableV.tableHeaderView = self.circleV;
    
    [_circleV release];
}


//- (void)createHeadCollectionView
//{
//    // layout
//    UICollectionViewFlowLayout *HeadFlowLayout = [[UICollectionViewFlowLayout alloc] init];
//    HeadFlowLayout.itemSize = CGSizeMake(BOUNDS_WIDTH, 194);
//    HeadFlowLayout.minimumInteritemSpacing = 0;
//    HeadFlowLayout.minimumLineSpacing = 0;
//    HeadFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    
//    
//    // collection
//    self.headCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, 194) collectionViewLayout:HeadFlowLayout];
//    self.headCollectionView.backgroundColor = [UIColor whiteColor];
//    self.headCollectionView.delegate = self;
//    self.headCollectionView.dataSource = self;
//    self.headCollectionView.pagingEnabled = YES;
//    self.headCollectionView.showsHorizontalScrollIndicator = NO;
//    
//    
//    [self.headCollectionView registerClass:[HeadCollectionViewCell class] forCellWithReuseIdentifier:@"HeadCollectionViewCell"];
//
//    
//    // 把头视图设置轮播图
//    self.tableV.tableHeaderView = self.headCollectionView;
//    
//    [_headCollectionView release];
//    [HeadFlowLayout release];
//    
//    
//    // 创建小圆点
//    self.headPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(275, 170, 100, 14)];
//    self.headPageControl.backgroundColor = [UIColor clearColor];
//    [self.tableV addSubview:self.headPageControl];
//    self.headPageControl.numberOfPages = 6;
//    self.headPageControl.tag = 1000;
//    [_headPageControl release];
//    
//    
//    
//    // 2.1自动滚(创建定时器)
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoRoll) userInfo:nil repeats:YES];
//    
//    // 1.1无限滚动(在最后添加第一页的内容)
//    UIImageView *lastimgView = [[UIImageView alloc] initWithFrame:CGRectMake(BOUNDS_WIDTH *self.HeadArr.count, 0, BOUNDS_WIDTH, 194)];
//    
//    
//    // 取第一张图片
//    [lastimgView sd_setImageWithURL:[NSURL URLWithString:self.HeadArr.firstObject]];
//    
//    [self.contentView addSubview:lastimgView];
//    [lastimgView release];
//    
//}
//
//
//#pragma mark - 滚动协议方法(手动图的时候,小圆点也动)
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
//{
//    
//    // 无限滚动1.2
//    if (self.headCollectionView.contentOffset.x / BOUNDS_WIDTH == self.HeadArr.count) {
//        self.headCollectionView.contentOffset = CGPointZero;
//    }
//    
//    
//    //获取页面控制器
//    self.headPageControl = [self.contentView viewWithTag:1000];
//    //修改小圆点
//    self.headPageControl.currentPage = self.headCollectionView.contentOffset.x / BOUNDS_WIDTH;
//    
//}
//
//
//#pragma mark - 自动滚(collectionView)
//// 2.2自动滚(定时器方法)
//- (void)autoRoll
//{
//    NSIndexPath *currentIndexPath = [self.headCollectionView indexPathsForVisibleItems].lastObject;
//    NSInteger nextItem = currentIndexPath.item + 1;
//    
//    if (currentIndexPath.item == self.HeadArr.count - 1) {
//        nextItem = 1;
//    }
//    
////    
////    [self.headCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:nextItem inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
//}
//
//
//#pragma mark - 滚啊滚
////2.3自动滚 当滚动的时候小圆点也动 当滚到最后一张的时候,重新滚到头
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (self.headCollectionView.contentOffset.x / BOUNDS_WIDTH == _HeadArr.count) {
//        self.headCollectionView.contentOffset = CGPointZero;
//    }
//    
//    self.headPageControl = [self.contentView viewWithTag:1000];
//    self.headPageControl.currentPage = self.headCollectionView.contentOffset.x / BOUNDS_WIDTH;
//}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.backgroundColor = [UIColor greenColor];
    // 建立tableview
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BOUNDS_WIDTH, BOUNDS_HEIGHT - 64 - 40 - 49 ) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    self.tableV.showsVerticalScrollIndicator = NO;
    self.tableV.rowHeight = 80;
    [self.contentView addSubview:self.tableV];
    // 注册重用标识
    [self.tableV registerClass:[MainTableViewCell class] forCellReuseIdentifier:@"MainTableViewCell"];
    [self.tableV registerClass:[ArrayMainTableViewCell class] forCellReuseIdentifier:@"ArrayMainTableViewCell"];
    // 释放
    [_tableV release];
    
    
}

#pragma mark - 点击进入详情界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"AAA");
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    

    InformationModel *inforModel = _Arr[indexPath.row];
    detailVC.url = inforModel.url;
    
    
    
    [self.navi pushViewController:detailVC animated:YES];
}



//#pragma mark - collectionView两个必须写的方法
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    return self.HeadArr.count;
//}
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    HeadCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeadCollectionViewCell" forIndexPath:indexPath];
//    HeadModel *headModel = _HeadArr[indexPath.item];
//    [cell.headImgView sd_setImageWithURL:[NSURL URLWithString:headModel.image]];
//    
//    return cell;
//}



#pragma mark - tableView两个必须写的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
    
    InformationModel *inforModel = _Arr[indexPath.row];

    
    if ([inforModel.type isEqualToNumber:@(3)]) {
        ArrayMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ArrayMainTableViewCell" forIndexPath:indexPath];
        
        
        cell.titleLab.text = inforModel.title;
        // NSNumber 转换成string
        //    NSString *countStr = [inforModel.count stringValue];
        NSString *countStr1=[NSString stringWithFormat:@"%@",inforModel.count];
        if ([countStr1 isEqualToString:@"0"]) {
            cell.countLab.text = @"抢沙发";
        }else{
            cell.countLab.text = [NSString stringWithFormat:@"评论%@",countStr1];
        };
        cell.timeLab.text = inforModel.pubDate;
        
        [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:inforModel.imageLeft]];
        [cell.midImgView sd_setImageWithURL:[NSURL URLWithString:inforModel.imageMid]];
        [cell.rightImgView sd_setImageWithURL:[NSURL URLWithString:inforModel.imageRight]];
        return cell;

        
    } else {
        
        MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainTableViewCell" forIndexPath:indexPath];
        
        
        cell.titleLab.text = inforModel.title;
        
       
        if ([inforModel.count isEqualToNumber:@(0)]) {
            cell.countLab.text = @"抢沙发";
        }else{
            cell.countLab.text = [NSString stringWithFormat:@"评论%@",inforModel.count];
        };
        cell.timeLab.text = inforModel.pubDate;
        NSLog(@"%@",inforModel.imageMain);
        [cell.leftImgView sd_setImageWithURL:[NSURL URLWithString:inforModel.imageMain]];
        
        
        return cell;

    }
    
    
}

#pragma mark - 分别设置数组图片跟单行图片的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationModel *inforModel = _Arr[indexPath.row];
    if ([inforModel.type isEqualToNumber:@(3)]) {
        return 120;
        
    }else{
        return 80;
    }
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
